package com.sjc.counter.v1;

import com.sjc.counter.entity.RequestInfo;
import com.sjc.counter.entity.RequestStat;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @Classname Aggregator
 * @Description 根据原始数据计算统计数据
 * @Date 2020/5/1 13:47
 * @Created by SangJiacun
 */
public class Aggregator {

    public static RequestStat aggregate(List<RequestInfo> requestInfos, long durationInmillis){
        double maxRespIime = Double.MIN_VALUE;
        double minRespTime = Double.MAX_VALUE;
        double avgRespTime = -1;
        double p999RespTime = -1;
        double p99RespTime = -1;
        double sumRespTime = 0;
        long count = 0;

        for(RequestInfo requestInfo : requestInfos){
            ++count;
            double responseTime = requestInfo.getResponseTime();
            if(maxRespIime < responseTime){
                maxRespIime = responseTime;
            }
            if(minRespTime >  responseTime){
                minRespTime = responseTime;
            }
            sumRespTime += responseTime;
        }

        if(count != 0 ){
            avgRespTime = sumRespTime / count;
        }

        long tps = (long)(count / durationInmillis * 1000);

        Collections.sort(requestInfos, new Comparator<RequestInfo>() {
            @Override
            public int compare(RequestInfo o1, RequestInfo o2) {
                double diff = o1.getResponseTime() - o2.getResponseTime();
                if(diff < 0.0){
                    return -1;
                }else if(diff > 0.0){
                    return 1;
                }else{
                    return 0;
                }
            }
        });

        int idx999 = (int)(count*0.999);
        int idx99 = (int)(count*0.99);
        if(count != 0){
            p999RespTime = requestInfos.get(idx999).getResponseTime();
            p99RespTime = requestInfos.get(idx99).getResponseTime();
        }

        RequestStat requestStat = new RequestStat();
        requestStat.setMaxResponseTime(maxRespIime);
        requestStat.setMinResponseTime(minRespTime);
        requestStat.setAvgResponseTime(avgRespTime);
        requestStat.setP999ResponseTime(p999RespTime);
        requestStat.setP99ResponseTime(p99RespTime);
        requestStat.setCount(count);
        requestStat.setTps(tps);
        return requestStat;
    }
}
